import axios from "axios";

const myaxios = axios.create({
  baseURL: "http://127.0.0.1:8888/api/private/v1/", // baseURL会自动加在请求地址上
});

// 登录请求
export function login(data) {
  return myaxios({
    method: "post",
    url: "login",
    data,
  });
}

//左侧菜单
export function menus() {
  return myaxios({
    method: "get",
    url: "menus",
  });
}

//用户列表
export function users(params) {
  return myaxios({
    method: "get",
    url: "users",
    params,
  });
}

//添加用户
export function addusers(data) {
  return myaxios({
    method: "post",
    url: "users",
    data,
  });
}
//删除用户
export function delusers(usermsg) {
  return myaxios({
    method: "delete",
    url: `users/${usermsg.id}`,
  });
}
//修改状态
export function changestate(data) {
  return myaxios({
    method: "put",
    url: `users/${data.id}/state/${data.mg_state}`,
  });
}
//修改用户信息
export function editUserMsg(editmsg) {
  return myaxios({
    method: "put",
    url: `users/${editmsg.id}`,
    data: editmsg,
  });
}
//分配用户角色
export function giveUserRole(id , rid) {
  return myaxios({
    method: "put",
    url: `users/${id}/role`,
    data:{
      rid
    }
  });
}


//权限列表
export function rightsList(data) {
  return myaxios({
    method: "get",
    url: `rights/${data}`
  });
}
//角色列表
export function getroleList() {
  return myaxios({
    method: "get",
    url: 'roles'
  });
}
//添加角色
export function addrole(data) {
  return myaxios({
    method: "post",
    url: 'roles',
    data
  });
}

//删除角色
export function deleterole(id) {
  return myaxios({
    method: "delete",
    url: `roles/${id}`,
  });
}

//编辑角色
export function editrole(data) {
  return myaxios({
    method: "put",
    url: `roles/${data.id}`,
    data
  });
}

//角色授权
export function rolepower(id , rids) {
  return myaxios({
    method: "post",
    url: `roles/${id}/rights`,
    data: {
      rids
    }
  });
}

//商品分类数据列表
export function categorieslist(params) {
  return myaxios({
    method: "get",
    url: 'categories',
    params
  });
}
//添加分类
export function addcategories(data) {
  return myaxios({
    method: "post",
    url: `categories`,
    data
  });
}


//删除分类
export function delcategories(id) {
  return myaxios({
    method: "delete",
    url: `categories/${id}`
  });
}


//获取参数列表
export function getparameterlist(id , params) {
  return myaxios({
    method: "get",
    url: `categories/${id}/attributes`,
    params: {
      sel: params
    }
  });
}

//添加参数或属性
export function addparamsOrattr(id , data) {
  return myaxios({
    method: "post",
    url: `categories/${id}/attributes`,
    data
  });
}


//获取商品列表
export function getGoodsList(params) {
  return myaxios({
    method: "get",
    url: `goods`,
    params
  });
}

//删除商品
export function delGoodsList(id) {
  return myaxios({
    method: "delete",
    url: `goods/${id}`
  });
}
//添加商品
export function addgoods(data) {
  return myaxios({
    method: "post",
    url: `goods`,
    data
  });
}
// 添加请求拦截器
myaxios.interceptors.request.use(
  function (config) {
    //设置请求头，添加token验证的Authorization字段
    config.headers.Authorization = window.sessionStorage.getItem("token");
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);
